package com.mobiletrain.org.motherkitch.shequ;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class SheQuActivity extends ActionBarActivity {

    private ScrollView pullToRefreshScrollView;
    private ProgressBar bar;
    private int page = 1;
    private int limit = 20;
    private String path;
    private ListView list;
    private SheQuDetailClass sheQuDetailClass;
    private SheQuDetailReplyClass sheQuDetailReplyClass;
    private ArrayList<SheQuDetailClass> sheQuDetailClassesData;
    private SheQuDetailAdapter adapter;
    private String mobile;
    private ListViewToScorllView listView;
    private ArrayList<SheQuDetailReplyClass> sheQuDetailReplyClassesData;
    private SheQuDetailReplyAdapter replyAdapter;
    private TextView tv_city;
    private ImageView iv_image;
    private TextView tv_nickname;
    private TextView tv_date;
    private TextView tv_age_str;
    private View view;
    private ImageView loadingImage;
    private ListViewToScorllView listView1;
    private ListViewToScorllView listView2;
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_she_qu);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String id1 = getIntent().getStringExtra("id");
        mobile = id1.substring(0, 2);
        path = "http://www.qubaobei.com/ios/api/adr_view.php?mobile=" + mobile + "&id=" + id1 + "&limit=" + page * limit + "&order=1";

       // loadingImage = (ImageView) findViewById(R.id.image);
        listView1 = (ListViewToScorllView) findViewById(R.id.listView1);
        listView2 = (ListViewToScorllView) findViewById(R.id.listView2);
        bar = (ProgressBar) findViewById(R.id.progressBar);
         pullToRefreshScrollView = (ScrollView) findViewById(R.id.pullToRefreshScrollView);
       // pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        sheQuDetailClassesData = new ArrayList<SheQuDetailClass>();
        sheQuDetailReplyClassesData = new ArrayList<SheQuDetailReplyClass>();
        adapter = new SheQuDetailAdapter(SheQuActivity.this, sheQuDetailClassesData);
        replyAdapter = new SheQuDetailReplyAdapter(SheQuActivity.this, sheQuDetailReplyClassesData);
        sheQuDetail();


     /*   pullToRefreshScrollView.setOnPullEventListener(new PullToRefreshBase.OnPullEventListener<ScrollView>() {
            @Override
            public void onPullEvent(PullToRefreshBase<ScrollView> refreshView, PullToRefreshBase.State state, PullToRefreshBase.Mode direction) {
                Toast.makeText(SheQuActivity.this, "正在加载...", Toast.LENGTH_SHORT).show();
                page++;
                sheQuDetail();
            }
        });*/
         view = LayoutInflater.from(this).inflate(R.layout.shequdetailhead_list_item, null);
    //    list = (ListViewToScorllView) view.findViewById(R.id.list);
        tv_city = (TextView) view.findViewById(R.id.city);
        iv_image = (ImageView) view.findViewById(R.id.image);
        tv_nickname = (TextView) view.findViewById(R.id.nickname);
        tv_date = (TextView) view.findViewById(R.id.date);
        tv_age_str = (TextView) view.findViewById(R.id.age_str);
    }


    public void sheQuDetail() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<Object>() {
            String big;

            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String json = objectResponseInfo.result.toString();
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONObject data = jsonObject.getJSONObject("data");
                    String is_lock = data.getString("is_lock");
                    String b_id = data.getString("b_id");
                    String nickname = data.getString("nickname");
                    String o_user_id = data.getString("o_user_id");
                    String daren = data.getString("daren");
                    String level = data.getString("level");
                    String re_num = data.getString("re_num");
                    String age_str = data.getString("age_str");
                    String user_id = data.getString("user_id");
                    String title = data.getString("title");
                    String avatar = data.getString("avatar");
                    JSONArray contentArray = data.getJSONArray("content");
                    String content = null;
                    for (int i = 0; i < contentArray.length(); i++) {
                        JSONObject jsonObject1 = contentArray.getJSONObject(i);
                        String type = jsonObject1.getString("type");
                        if (type.equals("1")) {
                            String content1 = jsonObject1.getString("content");
                            content = Html.fromHtml(content1).toString();
                            String a = jsonObject1.getString("a");
                        } else if (type.equals("2")) {
                            big = jsonObject1.getString("big");
                            int width = jsonObject1.getInt("width");
                            int height = jsonObject1.getInt("height");
                            content = jsonObject1.getString("content");
                        }
                        String qz_name = data.getString("qz_name");
                        String show_date = data.getString("show_date");
                        String city = data.getString("city");
                        sheQuDetailClass = new SheQuDetailClass();
                        sheQuDetailClass.setTitle(title);
                        sheQuDetailClass.setAge_str(age_str);
                        sheQuDetailClass.setContent(content);
                        sheQuDetailClass.setNickname(nickname);
                        sheQuDetailClass.setShow_date(show_date);
                        sheQuDetailClass.setBig(big);
                        sheQuDetailClass.setType(type);
                        sheQuDetailClass.setCity(city);
                        sheQuDetailClassesData.add(sheQuDetailClass);
                        tv_nickname.setText(nickname);
                        tv_age_str.setText(age_str);
                        tv_city.setText(city);
                        tv_date.setText(show_date);
                        BitmapUtils bitmapUtils = ImageLoad.getBitmapUtils(SheQuActivity.this);
                        bitmapUtils.display(iv_image, avatar);
                    }
                    JSONArray reply = jsonObject.getJSONArray("reply");
                    for (int j = 0; j < reply.length(); j++) {
                        JSONObject jsonObject2 = reply.getJSONObject(j);
                        String id1 = jsonObject2.getString("id");
                        String content1 = jsonObject2.getString("content");
                        String dated = jsonObject2.getString("dated");
                        String nickname1 = jsonObject2.getString("nickname");
                        String city1 = jsonObject2.getString("city");
                        String avatar1 = jsonObject2.getString("avatar");
                        String age_str1 = jsonObject2.getString("age_str");
                        String show_date1 = jsonObject2.getString("show_date");
                        String pic = jsonObject2.getString("pic");
                        sheQuDetailReplyClass = new SheQuDetailReplyClass();
                        sheQuDetailReplyClass.setNickname(nickname1);
                        sheQuDetailReplyClass.setAge_str(age_str1);
                        sheQuDetailReplyClass.setContent(content1);
                        sheQuDetailReplyClass.setCity(city1);
                        sheQuDetailReplyClass.setAvatar(avatar1);
                        sheQuDetailReplyClass.setShow_date(show_date1);
                        sheQuDetailReplyClass.setId(id1);
                        sheQuDetailReplyClass.setPic(pic);
                        sheQuDetailReplyClassesData.add(sheQuDetailReplyClass);
                    }
                   // loadingImage.setVisibility(View.INVISIBLE);
                    replyAdapter.notifyDataSetChanged();
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                listView1.addHeaderView(view);
                listView1.setAdapter(adapter);

                listView2.setAdapter(replyAdapter);
                bar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(SheQuActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_she_qu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
